package com.pn.controller;

import com.pn.dto.AssignAuthDto;
import com.pn.dto.RolePageList;
import com.pn.entity.Auth;
import com.pn.entity.Role;
import com.pn.service.RoleService;
import com.pn.utils.PageBean;
import com.pn.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;


    //查询所有角色
    @GetMapping("/role-list")
    public Result roleList(){
        List<Role> roleList = roleService.roleList();
        return Result.ok(roleList);
    }

    //角色列表
    @GetMapping("role-page-list")
    public Result rolePageList(RolePageList rolePageList){
        PageBean<Role> list = roleService.rolePageList(rolePageList);
        return Result.ok(list);
    }

    //添加角色
    @PostMapping("/role-add")
    public Result roleAdd(@RequestBody Role role){
        Result result =  roleService.roleAdd(role);
        return result;
    }

    //启用禁用角色
    @PutMapping("/role-state-update")
    public Result roleStateUpdate(@RequestBody Role role){
        Result result = roleService.roleStateUpdateByRid(role);
        return result;
    }


    //删除角色
    @DeleteMapping("/role-delete/{roleId}")
    public Result roleDelete(@PathVariable Integer roleId){
        Result result = roleService.roleDeleteByRid(roleId);
        return result;
    }

    //查询角色分配的所有权限菜单
    @GetMapping("/role-auth")
    public Result roleAuth(Integer roleId){
        List<Integer> authList = roleService.querRoleAuthIds(roleId);
        return Result.ok(authList);
    }

    //给角色添加权限
    @PutMapping("/auth-grant")
    public Result grantAuth(@RequestBody AssignAuthDto assignAuthDto){
        Result result = roleService.grantAuthAdd(assignAuthDto);
        return result;
    }

    //修改角色
    @PutMapping("lole-update")
    public Result updateRole(@RequestBody Role role){
        Result result = roleService.setDescByRid(role);
        return result;
    }


}
